<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CartController extends Controller
{

	 //购物车加入
      //接受ajax 数据 处理发来的库存id 规格id 套餐 id
	 public function getAa(Request $request)
    {
      // $data = $request->session()->all();
      // dd($data);
      $data = $request->all();
      //$data = session($data);
      //dd($data);
       // 商品 id
       $g_id = $data['g_id'];
      //库存的型号
       $size = $data['size'];
      //规格的id
      $color = $data['color'];
      //套餐的id
      $package_id = $data['pk'];
      //查到商品的价格
      $price = $data['price'];

      $num = 1;
        //dd($goods_id);
      // 把商品详情页所选的商品存入session 如果此时检测用户id不存在就将数据存入cart数据库中此时商品默认为一件
      // 查询数据库中;如果此时检测用户id存在就将数据存入cart数据库中
        $u_id = session('id');
        if($u_id != null)
          //存在就把添加的商品信息存入数据库
        {
          $res_cart = DB::table('cart')->where('g_id',$g_id)->get();
              if($res_cart == null){

                  $cartinfo = ['u_id'=>$u_id, 'g_id'=>$g_id,'size'=>$size,'color'=>$color,'package_id'=>$package_id, 'price'=>$price,'num'=>$num];

                  $cartss = DB::table('cart')->insert($cartinfo);
            //dd($res);
              }
        }




      $res = ['g_id'=>$g_id,'size'=>$size,'color'=>$color,'package_id'=>$package_id, 'price'=>$price, 'num'=>$num];

     // dd($res);

         $gd = session('cart');

         //dd($gd);
          if(empty($gd)){

              session()->push('cart', $res);
           //echo "string";
          }else{

              foreach ($gd as $k => $v) {
                   //var_dump($v['id']);
                 if($v['g_id'] == $g_id){
                  //echo "string";
                 die;
              }
               

            

         }
          // session('cart', $res);
         session()->push('cart', $res);
       }
      
      //session()->push('cart', $res);
      }
   



    /**
     * 前台购物车
     */
    public function getIndex(Request $request)
    {
    	//echo "string";
    	$data = $request->session()->all();
    	//dd($data);
    	 $cart = session('cart');
    	 //dd($cart);
    	 //判断购物车中是否有商品
    	 //计算  价格
    	$total = 0;
    	// 商品小计
        $xiaoji=0;
        // 商品套餐中的小计价格
        $taocan = 0;
			          
    	 
    	 if(empty($cart)){

    	 	$kong = "您的购物车空空的什么都没有赶紧去购物吧~~~~~";

    	 	return view('/home/cart',['kong'=>$kong,'data'=>$data]);

    	 	}else{
    	 	    $cartinfo = [];
	    		foreach ($cart as $k => $v) {
	    			    $cartinfo[$k]= $v;
	    				//dd($v);
	    			   // dd($cartinfo);
	    			    // 商品图片 名字插入到cartinfo
			    	    $goods_id = $cartinfo[$k]['g_id'];
						$guige = DB::table('guige')->where('gid',$goods_id)->first();
				    	$cartinfo[$k]['guige_image'] = $guige->image;
				    	$cartinfo[$k]['guige_proname'] = $guige->proname;
						
		    			//dd($cartinfo);
				    	// 查询套餐的id 查询套餐内部的套餐的商品
				    	if($cartinfo[$k]['package_id'] != 0 ){
				    	$package_id = $cartinfo[$k]['package_id'];
				    	$package = DB::table('package')->where('id',$package_id)->first();
				    	$packagegoods = DB::table('packagegoods')
				    					->join('goods','goods.id','=','packagegoods.tgsid')
				    					->where('peid',$package_id)
				    					->get();
				    	//dd($packagegoods);
						 $cartinfo[$k]['package'] = $package;
						 $cartinfo[$k]['packagegoods'] =$packagegoods;
			             $taocan =  $package->xprice;

			            }
			          // dd($package);
				    	//var_dump($package);die;
				    	//获取套餐内部的商品
				    	//查询商品图片
				    	//dd($cartinfo);		    	                
		    			// 单个商品的小计价格
			            $xiaoji = $cartinfo[$k]['num'] * $cartinfo[$k]['price']+$taocan;
					    
			            $total+=$xiaoji;
			            
			            $taocan = 0;

		    			}
		    			//return $cartinfo;
					    	
		    			$kong = [];
		    			//dd($kong);
		    			//dd($cartinfo);
    				 return view('/home/cart',['kong'=>$kong,'data'=>$data,'cartinfo'=>$cartinfo,'total'=>$total]);
		    	 	}

		   	
    }



    	//购物车删除商品

    	public function getCartdel(Request $request)
    	{
    		 $g_id = $request->input('g_id');
    		 $u_id = session('id');

			// dd($u_id);
    		 //删除数据库中的数据

			if(!empty($u_id)){
				$cc = DB::table('cart')->where('g_id',$g_id)->delete();
				//dd($cc);
			}

	    		 
	    	$data = [];
	    	//dd($data);
	    	$cart = session('cart');
    		//dd($cart);
    		if(empty($g_id)){
    			return false;
    			die;

    		}else{

			    	foreach ($cart as $k => $v) {

			    		if($v['g_id'] == $g_id){

			    			unset($cart[$k]);
			    		}else{
			    			$data[] = $cart[$k];

			    		}
			    	}

			    	session(['cart' => $data]);
	    		//dd($aa);
			 }


    		

    	}

    	//空 

    

   //用户登录后首页进入购物车
     public function getCart()
     {
     	//用户的id session
     	$u_id = session('id');

     	 //计算  价格
    	$total = 0;
    	// 商品小计
        $xiaoji=0;
        // 商品套餐中的小计价格
        $taocan = 0;

		if(!empty($u_id)){  

			$cartuser =  DB::table('cart')->where('u_id',$u_id)->get();   
			if($cartuser != null){	
				foreach ($cartuser as $key => $value) {
				  	$cartgoods = get_object_vars($value);
				  }  
			 $gd = session('cart');
			 $g_id =$cartgoods['g_id'];

			// dd($g_id);

          if(empty($gd)){

              session()->push('cart', $cartgoods);
              // dd($g_id);
           
              // foreach ($gd as $k => $v){
              // 	$v['g_id'] == $g_id;
              // }

          }else{

              foreach ($gd as $k => $v) {
                   
                 if($v['g_id'] != $g_id){
                  //echo "string";
                
              }
               

            

         }
          
         session()->push('cart', $cartgoods);
       }
      
	    	 
       		}	
		}
	    	
	return redirect('/home/cart');
     }

    
     public function getCartuser()
     {
     	return view('/home/cartuser');
     }




    

}